package com.thingcom.chinamobile.handler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ctg.ag.sdk.core.model.BaseApiRequest;
import com.ctg.ag.sdk.core.model.BaseApiResponse;

import java.io.UnsupportedEncodingException;

/**
 * @ClassName BaseAepClientWrapper
 * @Description AEP请求封装包装类
 * @Author zhou tao
 * @Date 2020/1/14 13:43
 * @Version 1.0
 **/
public abstract class BaseAepClientWrapper {

	/**
	 *
	 * @param request
	 * @param body
	 * @return
	 */
	public JSONObject excute(BaseApiRequest request, JSONObject body) {
		try {
			if (body != null) {
				request.setBody(JSONUtil.toJsonStr(body).getBytes("UTF8"));
			}
			BaseApiResponse response = handleRequest(request);
			return JSONUtil.parseObj(new String(response.getBody(), "UTF8"));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 *
	 * @param request
	 * @return
	 */
	public abstract BaseApiResponse handleRequest(BaseApiRequest request) throws Exception;
}
